import {
    allSginImprotExecl,
    allGetSginExeclTemp,
    fileDownState,
    allGetSginRelation,
} from '@/api/manage/sign'
import axios from 'axios/dist/axios.min'
import JSZip from 'jszip'
import FileSaver from 'file-saver'
const getFile = url => {
    return new Promise((resolve, reject) => {
        axios({
            method:'get',
            url,
            responseType: 'arraybuffer'
        }).then(data => {
            resolve(data.data)
        }).catch(error => {
            resolve();
           // reject(error.toString())
        })
    })
}
const sign = {
    actions: {
        allSginImprotExecl({ commit },data) {
            return new Promise((resolve, reject) => {
                allSginImprotExecl(data).then(response => {
                    let link = document.createElement('a');
                    var blob = new Blob([response],{ type: 'application/vnd.ms-excel'});
                    link.download = "导出签约.xls";
                    link.style.display = 'none'
                    link.href = URL.createObjectURL(blob);
                    document.body.appendChild(link)
                    link.click()
                    document.body.removeChild(link)
                    resolve(true);
                }).catch(error => {
                reject(error)
                })
            })
        },
        allGetSginExeclTemp({ commit },data) {
            return new Promise((resolve, reject) => {
                allGetSginExeclTemp(data).then(response => {
                    const url = response.data;
                    let link = document.createElement('a');
                    link.href = url;
                    link.download = "签约模板.xlsx";
                    link.style.display = 'none'
                    link.click()
                    document.body.removeChild(link)
                    resolve();
                }).catch(error => {
                reject(error)
                })
            })
        },
        fileDownState({ commit },data) {
            return new Promise((resolve, reject) => {
                fileDownState(data).then(response => {
                    resolve(response.data);
                }).catch(error => {
                reject(error)
                })
            })
        },
        allGetSginRelation({ commit },relationId) {
            return new Promise((resolve, reject) => {
                allGetSginRelation(relationId).then(res => {
                if (res.code === '1') {
                    resolve(res.data);
                }
                }).catch(error => {
                reject(error)
                })
            })
        },
        allSginDownContract({ commit },datas) {
            return new Promise((resolve, reject) => {
                var fileList = [];
                datas.forEach((item, i) => {
                    if (item.pactUrl&&item.pactUrl.indexOf('null') == -1) {
                        let pieName = item.idCard+'.pdf'
                        fileList.push({url:item.pactUrl,file_name:pieName})
                    }
                })
                var zip = new JSZip();
                const cache = {}
                let promises = [];
                if (fileList.length > 0) {
                    fileList.forEach(item => {
                        const promise = getFile(item.url).then(data => {
                            if (data) {
                                zip.file(item.file_name, data, { binary: true })
                                cache[item.file_name] = data
                            }
                        })
                        promises.push(promise)
                    })
                    Promise.all(promises).then(() => {
                        zip.generateAsync({ type: "blob" }).then(content => {
                            resolve(true);
                            FileSaver.saveAs(content, "签约合同.zip")
                        })
                    })
                } else {
                    resolve(false);
                }
            })
        }
    }
}
export default sign
